<?php
namespace app\beautiful\model\order;
use app\common\model\CommonModel;
use think\facade\Db;

class BuyOrderModel extends CommonModel{
    protected  $name = 'beautiful_buy_order';
    protected  $pk   = 'buy_order_id';
    protected $type=[
        'pay_info' => 'array',
    ];

    public function getPriceAttr($value,$data)
    {
        return round(($data['balance']*100 - $data['vip_balance']*100 - $data['coupon_money']*100 - $data['integral_balance']*100)/100,2);
    }

    public function getDiscountMoneyAttr($value,$data)
    {
        return round(($data['vip_balance']*100 + $data['coupon_money']*100 + $data['integral_balance']*100)/100,2);
    }

    public function getDiscountAttr($value,$data)
    {
        return round((($data['balance']*100 - $data['vip_balance']*100)/($data['balance']*100))*10,2);
    }

    public function getPayTypeMeanAttr($value,$data)
    {
        $type_means=[
            'weixin'=>'微信支付',
            'money'=>'余额支付'
        ];
        return $type_means[$data['pay_type']] ?? '其他支付方式';
    }
    public function getPayTimeAttr($value,$data){

        return date('Y-m-d H:i:s',$data['pay_time']);

    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_buy_order')->alias('a')
            ->where(['a.from_type'=>2])
            ->when(!empty($where['pay_time']),function ($query) use ($where){
                $query->where('a.pay_time','>',$where['pay_time']);
            })
            ->when(!empty($where['edit_time']),function ($query) use ($where){
                $query->where('a.edit_time','>',date('Y-m-d H:i:s',$where['edit_time']));
            })
            ->when(isset($where['is_paid']),function ($query) use ($where){
                $query->where('a.is_paid','=',$where['is_paid']);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where('a.status','=',$where['status']);
            })
            ->when(isset($where['is_settle']),function ($query) use ($where){
                $query->where('a.is_settle','=',$where['is_settle']);
            })
            ->when(!empty($where['>=edit_time']),function ($query) use ($where){
                $query->where('a.edit_time','>=',date('Y-m-d H:i:s',$where['>=edit_time']));
            })
            ->when(!empty($where['<=edit_time']),function ($query) use ($where){
                $query->where('a.edit_time','<=',date('Y-m-d H:i:s',$where['<=edit_time']));
            })
            ->field("a.*");
        $query=$query->order($order);
        if(empty($where['is_all'])){
            $query=$query->limit($limit)->page($page);
        }
        $list=$query->select()->toArray();
        return $list;
    }
    public static function user_order($where){
        $query = Db::name('beautiful_buy_order')->alias('a')
            ->join('beautiful_member b','a.member_id=b.member_id')
            ->where(['a.from_type'=>2])
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(!empty($where['pid']),function ($query) use ($where){
                $query->where(['b.pid'=>$where['pid']]);
            })
            ->when(isset($where['is_paid']),function ($query) use ($where){
                $query->where(['a.is_paid'=>$where['is_paid']]);
            })
            ->when(!empty($where['start_time']),function ($query) use ($where){
                $query->where('a.edit_time','>=',date('Y-m-d H:i:s',$where['start_time']));
            })
            ->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('a.edit_time','<=',date('Y-m-d H:i:s',$where['end_time']));
            })
            ->field("sum(a.pay_money-a.refund_money) as money,count(a.buy_order_id) as all_num,a.member_id");
        $info=$query->find();
        return $info;
    }
    public static function max_money($where){
        $query = Db::name('beautiful_buy_order')->alias('a')
            ->join('beautiful_member b','a.member_id=b.member_id')
            ->where(['a.from_type'=>2])
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(!empty($where['pid']),function ($query) use ($where){
                $query->where(['b.pid'=>$where['pid']]);
            })
            ->when(!empty($where['start_time']),function ($query) use ($where){
                $query->where('a.edit_time','>=',date('Y-m-d H:i:s',$where['start_time']));
            })
            ->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('a.edit_time','<=',date('Y-m-d H:i:s',$where['end_time']));
            })
            ->field("(a.pay_money-a.refund_money) as money,a.member_id")
            ->order(['money'=>'desc']);
        $info=$query->find();
        $money=0;
        if(!empty($info)){
            $money=$info['money'];
        }
        return $money;
    }
}
